package com.xmjs.baselibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.xmjs.baselibrary.utils.encode.AuthUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * SharedPreferences工具类
 */
public class SP {
    private static final String DBName = "com.xm.dqx";
    private SharedPreferences sp;

    public SP(Context context) {
        sp = context.getSharedPreferences(DBName, Context.MODE_PRIVATE);
    }

    public SharedPreferences getSP() {
        return sp;
    }

    public String getString(String key) {
        return AuthUtil.AesDecrypt(sp.getString(key, null));
    }

    public String getString(String key, String defValue) {
        return AuthUtil.AesDecrypt(sp.getString(key, defValue));
    }

    public void putString(String key, String value) {
        if (TextUtils.isEmpty(value))
            value = "";
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, AuthUtil.AesEncrypt(value));
        editor.apply();
    }

    public Boolean getBoolean(String key) {
        return sp.getBoolean(key, false);
    }

    public Boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public void putBoolean(String key, Boolean value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public Float getFloat(String key) {
        return sp.getFloat(key, 0f);
    }

    public void putFloat(String key, Float value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public Integer getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    public Integer getInt(String key) {
        return getInt(key, 0);
    }

    public void putInt(String key, Integer value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public Long getLong(String key) {
        return sp.getLong(key, 0);
    }

    public void putLong(String key, Long value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public Set<String> getStringSet(String key) {
        return sp.getStringSet(key, new HashSet<>());
    }

    public void putStringSet(String key, Set<String> value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putStringSet(key, value);
        editor.apply();
    }

    public void putObject(String key, Object value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value == null ? null : JSON.toJSONString(value));
        editor.apply();
    }

    public <T> T getObject(String key, Class<T> clazz) {
        String value = sp.getString(key, null);
        if (null == value) {
            return null;
        }
        return JSON.parseObject(value, clazz);
    }

    public void removeObject(String key) {
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    public void putList(String key, List value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value == null ? null : AuthUtil.AesEncrypt(JSON.toJSONString(value)));
        editor.apply();
    }

    public <T> List<T> getList(String key, Class<T> clazz) {
        String value = sp.getString(key, null);
        if (null == value) {
            return null;
        }
        return JSON.parseArray(AuthUtil.AesDecrypt(value), clazz);
    }
}